package com.edu.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.edu.admin.entity.SysSchoolProduct;
import com.edu.admin.enums.ExceptionEnum;
import com.edu.admin.service.SchoolProductService;
import com.edu.admin.common.RestResult;
import com.edu.admin.vo.SchoolProductVo;
import com.github.pagehelper.PageInfo;


/**
 * 校级产品管理
 *  
 * @author Wangjiong
 * 
 */
@RestController
@RequestMapping(value = "/schoolproduct", name = "校级产品管理")
public class SchoolProductController {
	@Autowired
	private SchoolProductService	schoolProductService;
	
	/**
	 *
	 * @apiGroup schoolproduct
	 * @api {get} /schoolproduct/list 获取学校可用产品列表
	 * @apiVersion 1.0.0
	 * @apiName getSchoolProductList
	 *
	 *
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
			{
			    "code": 200,
			    "msg": "success",
			    "data": [
			        {
			            "schoolId": 1,
			            "productId": 1,
			            "schoolName": "测试学校",
			            "productName": "用户管理中心",
			            "useType": 1,
			            "startDate": "2020-07-01",
			            "endDate": "2020-08-31"
			        },
			        {
			            "schoolId": 1,
			            "productId": 2,
			            "schoolName": "测试学校",
			            "productName": "互动课堂",
			            "useType": 2,
			            "startDate": "2020-07-01",
			            "endDate": "2020-08-26"
			        },
			        {
			            "schoolId": 1,
			            "productId": 3,
			            "schoolName": "测试学校",
			            "productName": "题库中心",
			            "useType": 1,
			            "startDate": "2020-07-01",
			            "endDate": "2020-08-31"
			        }
			    ]
			}
	 *
	 *
	 */
    @RequestMapping(value = "list", name = "学校可用产品列表", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('/schoolproduct/list')")
    public RestResult getSchoolProductList(@RequestParam(name = "schoolId") Long schoolId) 
			throws Exception {

		List<SchoolProductVo> schoolProductList = schoolProductService.getSchoolProductListBySchoolId(schoolId);
        return RestResult.ok(schoolProductList);
    }
    

    /**
	 *
	 * @apiGroup schoolproduct
	 * @api {get} /schoolproduct/listAll 获取学校所有产品列表（包括停用学校产品）
	 * @apiVersion 1.0.0
	 * @apiName getAllSchoolProductList
	 *
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
			{
			    "code": 200,
			    "msg": "success",
			    "data": [
			        {
			            "schoolId": 1,
			            "productId": 1,
			            "schoolName": "测试学校",
			            "productName": "用户管理中心",
			            "useType": 1,
			            "startDate": "2020-07-01",
			            "endDate": "2020-08-31"
			        },
			        {
			            "schoolId": 1,
			            "productId": 2,
			            "schoolName": "测试学校",
			            "productName": "互动课堂",
			            "useType": 2,
			            "startDate": "2020-07-01",
			            "endDate": "2020-08-26"
			        },
			        {
			            "schoolId": 1,
			            "productId": 3,
			            "schoolName": "测试学校",
			            "productName": "题库中心",
			            "useType": 1,
			            "startDate": "2020-07-01",
			            "endDate": "2020-08-31"
			        }
			    ]
			}
	 *
	 *
	 */
    @RequestMapping(value = "listAll", name = "学校所有产品列表", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('/schoolproduct/listAll')")
    public RestResult getAllSchoolProductList(@RequestParam(name = "schoolId") Long schoolId,
    		@RequestParam(value = "productName", required = false) String productName,
			@RequestParam(value = "useType", required = false) Integer useType) 
			throws Exception {

		List<SchoolProductVo> schoolProductList = schoolProductService.getAllSchoolProductListBySchoolId(schoolId,productName,useType);
        return RestResult.ok(schoolProductList);
    }
    
    /**
	 *
	 * @apiGroup schoolproduct
	 * @api {get} /schoolproduct/listAllPage 获取所有学校产品列表（包括停用学校产品） (分页)
	 * @apiVersion 1.0.0
	 * @apiName getAllSchoolProductListPage
	 *
	 *
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "total": 2,
		        "list": [
		            {
		                "schoolId": 1,
		                "productId": 1,
		                "schoolName": "测试学校",
		                "productName": "用户管理中心",
		                "startDate": "2020-05-02",
		                "endDate": "2020-08-02"
		            },
		            {
		                "schoolId": 1,
		                "productId": 2,
		                "schoolName": "测试学校",
		                "productName": "互动课堂",
		                "startDate": "2020-05-02",
		                "endDate": "2020-08-02"
		            }
		        ],
		        "pageNum": 1,
		        "pageSize": 10,
		        "size": 2,
		        "startRow": 1,
		        "endRow": 2,
		        "pages": 1,
		        "prePage": 0,
		        "nextPage": 0,
		        "isFirstPage": true,
		        "isLastPage": true,
		        "hasPreviousPage": false,
		        "hasNextPage": false,
		        "navigatePages": 8,
		        "navigatepageNums": [
		            1
		        ],
		        "navigateFirstPage": 1,
		        "navigateLastPage": 1,
		        "firstPage": 1,
		        "lastPage": 1
		    }
		}
	 *
	 *
	 */
   @RequestMapping(value = "listAllPage", name = "学校素有产品列表(分页)", method = RequestMethod.GET)
   @PreAuthorize("hasAuthority('/schoolproduct/listAllPage')")
   public RestResult getAllSchoolProductListPage(@RequestParam(name = "schoolId") Long schoolId,
		    @RequestParam(value = "productName", required = false) String productName,
			@RequestParam(value = "useType", required = false) Integer useType,
   		@RequestParam(name = "pageNum", required = false) Integer pageNum,
			@RequestParam(name = "pageSize", required = false) Integer pageSize) 
			throws Exception {

		PageInfo<SchoolProductVo> pageInfo = schoolProductService.getAllSchoolProductListPageBySchoolId(pageNum,pageSize, schoolId, productName, useType);
        return RestResult.ok(pageInfo);
   }
    
    /**
	 * 保存学校产品信息
	 * 
	 */
	/**
	 *
	 * @apiGroup schoolproduct
	 * @api {get} /schoolproduct/save 保存学校产品信息
	 * @apiVersion 1.0.0
	 * @apiName saveSchoolProduct
	 *
	 * @apiParam {Long} schoolId 学校id
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": 1
		}
	 *
	 *
	 */
	@RequestMapping(value = "save", name = "保存学校产品信息", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('/schoolproduct/save')")
	public RestResult saveSchoolProduct(@Valid @RequestBody SysSchoolProduct schoolProduct) throws Exception{
		Map<String, Object> result = new HashMap<>();
				
		schoolProductService.saveSchoolProduct(schoolProduct);
	
		return RestResult.ok(result);
	}
	
	/**
	 *
	 * @apiGroup schoolproduct
	 * @api {get} /schoolproduct/info 获取学校产品信息
	 * @apiVersion 1.0.0
	 * @apiName getSchoolProduct
	 *
	 * @apiParam {Long} schoolId 学校id
	 * @apiParam {Long} productId 产品id
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "schoolId": 1,
		        "productId": 1,
		        "schoolName": "测试学校",
		        "productName": "用户管理中心",
		        "useType": 1,
		        "startDate": "2020-07-01",
		        "endDate": "2020-08-31"
		    }
		}
	 *
	 *
	 */
	@RequestMapping(value = "info", name = "学校产品信息", method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('/schoolproduct/info')")
	public RestResult getSchoolProduct(@RequestParam(name = "schoolId") Long schoolId,
									@RequestParam(name = "productId") Long productId) throws Exception{
		SchoolProductVo schoolProductVo = null;
		
		SysSchoolProduct ssp = schoolProductService.getSchoolProductBySchoolIdAndProductId(schoolId,productId);
		
		if (ssp!=null)
		{
			schoolProductVo = new SchoolProductVo();
			BeanUtils.copyProperties(ssp,schoolProductVo);
		}
		return RestResult.ok(schoolProductVo);
	}
	
	/**
	 *
	 * @apiGroup schoolproduct
	 * @api {post} /schoolproduct/delete 删除学校产品
	 * @apiVersion 1.0.0
	 * @apiName deleteSchoolProdcut
	 *
	 * @apiParam {Map} dataMap 
	 * @apiParamExample 请求示例（json）
	 * {
		    "schoolId": 2
		}
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": 1
		}
	 *
	 *
	 */
	  @RequestMapping(value="/delete" , method = RequestMethod.POST)
	  @PreAuthorize("hasAuthority('/schoolproduct/delete')")
	  public RestResult deleteSchoolProdcut(@RequestBody Map<String,Object> dataMap) throws Exception {
	  	   
	     	Long schoolId = MapUtils.getLong(dataMap, "schoolId");
	     	
	     	if (schoolId == null || schoolId<=0)
	     	{
	     		return RestResult.error(ExceptionEnum.BADREQUEST.getCode(),ExceptionEnum.BADREQUEST.getMsg());
	     	}
	  	    			
		  	int isOk = schoolProductService.deleteSchoolProductBySchoolId(schoolId);
		  	if (isOk>0)
			{
				return RestResult.ok(isOk);
			}
			else
			{
				return RestResult.error();
			}
	  }


}
